import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.LinkedList;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;

public class Stats extends JFrame implements Runnable, ActionListener{

	private static final long serialVersionUID = 1L;
	private int runningTime;
	private double gemVerwerkingsSnelheid;
	private double maxSpeed;
	private JTextField totalData_txt;
	private JTextField gemVerwerkingsSnelheid_txt;
	private JTextField maxSpeed_txt;
	private JTextField runningTime_txt;
	private JTextField stationFiles_txt;
	private JTextField tempratureCorrections_txt;
	private JTextField missingValuesCorrected_txt;
	private JTextField correctData_txt;
	private JTextField huidigeSnelheid_txt;

	private JTextField stationid_txt;
	private DatabaseThread db;
	
	private DefaultTableModel model;
	private JTable table;
	private JTextField stationlimit_txt;
	private JTextField droppedmeasures_txt;
	private int totalMeasurementsWrittenToFile;
	
	public Stats(DatabaseThread db) {
		this.db = db;
		gemVerwerkingsSnelheid = 0;
		runningTime = 0;
		totalMeasurementsWrittenToFile = 0;
		maxSpeed = 0;
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE)
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
		);
		
		JPanel panel = new JPanel();
		tabbedPane.addTab("Resultaten Generator", null, panel, null);
		
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("Resultaten queries", null, panel_1, null);
		
		JPanel panel_2 = new JPanel();
		tabbedPane.addTab("Informatie", null, panel_2, null);
		
		getContentPane().setLayout(groupLayout);
		JLabel runningTime_lbl = new JLabel("Totale uptime (sec):");
		runningTime_txt = new JTextField();
		JLabel huidigeSnelheid_lbl = new JLabel("Huidige verwerkingssnelheid (verwerking/seconde):");
		huidigeSnelheid_txt = new JTextField();
		JLabel gemVerwerkingsSnelheid_lbl = new JLabel("Gem. verwerkingssnelheid (verwerking/seconde:)");
		gemVerwerkingsSnelheid_txt = new JTextField();
		JLabel maxSpeed_lbl = new JLabel("Max verwerkingssnelheid (verwerking/seconde):");
		maxSpeed_txt = new JTextField();
		JLabel stationFiles_lbl = new JLabel("Station files received:");
		stationFiles_txt = new JTextField();
		JLabel tempratureCorrections_lbl = new JLabel("Tempratures corrected:");
		tempratureCorrections_txt = new JTextField();
		JLabel missingValuesCorrected_lbl = new JLabel("Missing data corrected:");
		missingValuesCorrected_txt = new JTextField();
		JLabel correctData_lbl = new JLabel("Correct data(no missing/unreal temp:)");
		correctData_txt = new JTextField();
		JLabel totalData_lbl = new JLabel("Total measurements:");
		totalData_txt = new JTextField();
		JLabel droppedmeasures_lbl = new JLabel("Measurements dropped:");
		droppedmeasures_txt = new JTextField();
		JLabel stationid_lbl = new JLabel("Get data from station:");
		stationid_txt = new JTextField(10);
		JLabel stationlimit_lbl = new JLabel("Result limit:");	
		stationlimit_txt = new JTextField(10);
		JButton stationid_btn = new JButton("Search");

		

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		panel.setLayout(new GridLayout(0,2));
		panel.add(runningTime_lbl);
		panel.add(runningTime_txt);
		panel.add(huidigeSnelheid_lbl);
		panel.add(huidigeSnelheid_txt);
		panel.add(gemVerwerkingsSnelheid_lbl);
		panel.add(gemVerwerkingsSnelheid_txt);
		panel.add(maxSpeed_lbl);
		panel.add(maxSpeed_txt);
		panel.add(stationFiles_lbl);
		panel.add(stationFiles_txt);
		panel.add(tempratureCorrections_lbl);
		panel.add(tempratureCorrections_txt);
		panel.add(missingValuesCorrected_lbl);
		panel.add(missingValuesCorrected_txt);
		panel.add(correctData_lbl);
		panel.add(correctData_txt);
		panel.add(totalData_lbl);
		panel.add(totalData_txt);
		panel.add(droppedmeasures_lbl);
		panel.add(droppedmeasures_txt);
		
		Object[] header = {"Station","Date","Time","Dewp","Dewp","Stp","Slp","Visib","Wdsp","Prcp","Sndp","Frshtt","Cldc","Wnddir"};
		model = new DefaultTableModel();
	
		model.addColumn("Station");
		model.addColumn("Date");
		model.addColumn("Time");
		model.addColumn("Temp");
		model.addColumn("Dewp");
		model.addColumn("Stp");
		model.addColumn("Slp");
		model.addColumn("Visib");
		model.addColumn("Wdsp");
		model.addColumn("Prcp");
		model.addColumn("Sndp");
		model.addColumn("Frshtt");
		model.addColumn("Cldc");
		model.addColumn("Wnddir");
		
		table=new JTable(model);
		
		stationid_btn.addActionListener(this);
		
		JPanel panel1_inner = new JPanel();
		panel1_inner.add(stationid_lbl);		
		panel1_inner.add(stationid_txt);	
		panel1_inner.add(stationlimit_lbl);
		panel1_inner.add(stationlimit_txt);
		panel1_inner.add(stationid_btn);
		
		panel_1.setLayout(new BorderLayout());
		
		panel_1.add(new JScrollPane(table),BorderLayout.CENTER);
		panel_1.add(panel1_inner,BorderLayout.PAGE_START);
		

		this.pack();


		this.setVisible(true);
		this.setSize(600, 325);

		setVisible(true);
		
	}

	@Override
	public void run() {
			while(true) {		
				try {
					Thread.sleep(1000);		
					
					runningTime_txt.setText(String.valueOf(runningTime));
					
					
					
					huidigeSnelheid_txt.setText(String.valueOf(Server.measurementsWrittenToFile));
					gemVerwerkingsSnelheid_txt.setText(String.valueOf(gemVerwerkingsSnelheid));
					maxSpeed_txt.setText(String.valueOf(maxSpeed));
					stationFiles_txt.setText(String.valueOf(Server.stationFiles));
					tempratureCorrections_txt.setText(String.valueOf(Server.tempratureCorrections));
					missingValuesCorrected_txt.setText(String.valueOf(Server.missingValuesCorrected));
					droppedmeasures_txt.setText(String.valueOf(Server.droppedMeasurements));
					correctData_txt.setText(String.valueOf(totalMeasurementsWrittenToFile-Server.tempratureCorrections-Server.missingValuesCorrected));
					totalData_txt.setText(String.valueOf(totalMeasurementsWrittenToFile));
					
					runningTime++;
					if(Server.measurementsWrittenToFile != 0) {
						double gemV = Float.intBitsToFloat(totalMeasurementsWrittenToFile)/Float.intBitsToFloat(runningTime);
						BigDecimal bd = new BigDecimal(gemV).setScale(2, RoundingMode.HALF_EVEN);
						gemVerwerkingsSnelheid = bd.doubleValue();
					}	
						
						if(Server.measurementsWrittenToFile > maxSpeed) maxSpeed = Server.measurementsWrittenToFile;
						
						totalMeasurementsWrittenToFile += Server.measurementsWrittenToFile;
						
						Server.measurementsWrittenToFile = 0;
					
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
			int stn = Integer.parseInt(stationid_txt.getText());
			int limit = Integer.parseInt(stationlimit_txt.getText());
			LinkedList<Measurement> measurements = db.searchStationData(stn, limit);
			for(int i=0;i<model.getRowCount();i++) {
				model.removeRow(i);
			}
			for(Measurement m : measurements) {			
				Object[] rowData = {m.getStn(),m.getDate(),m.getTime(),m.getTemp(),
						m.getDewp(),m.getStp(),m.getSlp(),m.getVisib(),m.getWdsp(),
						m.getPrcp(),m.getSndp(),m.getFrshtt(),m.getCldc(),m.getWnddir()};	
				model.addRow(rowData);
			}
	}
}